package xyz.zdsoft.course_manage_module.service.imp;
import org.springframework.stereotype.Service;
import xyz.zdsoft.common.utils.Result;
import xyz.zdsoft.course_manage_module.mapper.InsertCourseGchMapper;
import xyz.zdsoft.course_manage_module.service.AdminInsertCourseService;
import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Service
public class AdminInsertCourseServiceImpl implements AdminInsertCourseService {
    @Resource
    private InsertCourseGchMapper insertCourseGchMapper;
    //查询该课程是否存在
    @Override
    public Integer SelHaveCourse(String courseName) {
        return insertCourseGchMapper.SelHaveCourse(courseName);
    }
    //如果课程不存在就新增课程
    @Override
    public void InsertCourse(String courseName) {
        insertCourseGchMapper.InsertCourse(courseName);
    }

    @Override
    public HashMap<String, Object> SelDateByClassRoom(String classRome) {
        return null;
    }

    //查询实训老师是否是在业状态
    @Override
    public String SelTrainTeacher(Long teacherId) {
        return insertCourseGchMapper.SelTrainTeacher(teacherId);
    }
    //查询实训老师是否存在
    @Override
    public Integer SelHaveTeacher(Long teacherId) {
        return insertCourseGchMapper.SelHaveTeacher(teacherId);
    }
    //新增课程安排
    @Override
    public Integer InsertCourseOn(HashMap<String, Object> map) {
        return insertCourseGchMapper.InsertCourseOn(map);
    }
    //获取最新增加的课程安排id
    @Override
    public Integer SelCourseOnIdBy(HashMap<String, Object> map) {
        return insertCourseGchMapper.SelCourseOnIdBy(map);
    }

    //新增课程安排与实训老师关系
    @Override
    public void InsertCourseAndTrainTeacher(Integer courseOnId, Long trainTeacherId) {
        insertCourseGchMapper.InsertCourseAndTrainTeacher(courseOnId,trainTeacherId);
    }

    @Override
    //获取课程id
    public Integer SelCourseId(String courseName) {
        if (insertCourseGchMapper.SelHaveCourse(courseName)==0){  //判断课程是否存在
            insertCourseGchMapper.InsertCourse(courseName);   //不存在就插入
        }
        return insertCourseGchMapper.SelCourseId(courseName);
    }

    //判断责任老师是否在实训老师之中
    public Integer dutyTeacherIdInTrainTeachers(Long dutyTeacherId,Long[] trainTeachers){
        int num=0;
        for (Long trainTeacher:trainTeachers){
            if (dutyTeacherId.equals(trainTeacher)){
                num=1;
            }
        }
        return num;
    }

    //判断时间是否过期
    public Integer updateTime(String projectDate,String signInTime) throws ParseException {
        int num=1;
        Date date=new Date();//获取当前时间
        String myDate=projectDate+" "+signInTime; //合并传入的日期与time
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //格式转换
        Date judgeDate = sdf.parse(myDate);
        if (date.compareTo(judgeDate)>=0){
            num=0;
        }
        return num;

    }


    // 判断时间是否冲突
    public Integer judgeDateTime(List<HashMap<String,Object>> dateMaps,String classRome,String projectDate,String signInTime,String signOutTime) throws ParseException {
        int num=1;
        if (dateMaps==null){
            return num;
        }
        for (HashMap<String,Object> dateMap:dateMaps) {
            String courseDate = String.valueOf(dateMap.get("project_date"));
            String courseOutTime =String.valueOf(dateMap.get("sign_out_time"));
            String courseInTime =String.valueOf(dateMap.get("sign_in_time"));
            System.out.println(courseInTime);
            //定义转换格式
            SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
            SimpleDateFormat sdf2 = new SimpleDateFormat("HH:mm:ss");
            //格式转换
            Date judgeDate1 = sdf1.parse(courseDate);//课程日期
            Date judgeDate2 = sdf1.parse(projectDate);//新增日期
            Date course_OutTime =sdf2.parse(courseOutTime);//课程签退时间
            Date course_InTime=sdf2.parse(courseInTime);//课程签到时间
            Date judgeInTime = sdf2.parse(signInTime);//新增签到时间
            Date judgeOutTime=sdf2.parse(signOutTime);//新增签退时间
            //时间对比

                if (judgeDate1.compareTo(judgeDate2) == 0) {
                        if (course_OutTime.compareTo(judgeInTime) > 0 || course_OutTime.compareTo(judgeInTime)==0 ) {
                        if (judgeOutTime.compareTo(course_InTime)>0 || judgeOutTime.compareTo(course_InTime)==0) {
                            num = 0;
                        }
                        else num=1;
                    }
                }

        }
        return num;

    }
    //判断输入的签退时间是否在签到时间之后
    public Integer judgeInAndOutTime(String signInTime,String signOutTime) throws ParseException {
        int num=1;
        //定义转换格式
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
        //格式转换
        Date judgeDate1 = sdf.parse(signInTime);
        Date judgeDate2 = sdf.parse(signOutTime);
        //时间对比
        if (judgeDate1.compareTo(judgeDate2) > 0 || judgeDate1.compareTo(judgeDate2) == 0){
            num=0;
        }
        return num;
    }
    //判断实训老师是否不存在或者停业
    public Integer haveTeacher(Long[] trainTeachers){
        int num = 0;
        for (Long trainTeacher:trainTeachers){
            if (insertCourseGchMapper.SelHaveTeacher(trainTeacher)>0){//判断实训老师是否存在
                if ("1".equals(insertCourseGchMapper.SelTrainTeacher(trainTeacher))){//判断老师是否停业
                    return trainTeacher.intValue();
                }
            }
            else {
                return trainTeacher.intValue();
            }
        }
        return num;
    }

    //总体逻辑判断
    public Result judgeInsertCourse(String projectDate,String courseName,
                                    Long dutyTeacherId,Long[] trainTeachers,
                                    String signInTime,String signOutTime,
                                    String classRome,String remark) throws ParseException {
        HashMap<String,Object> courseMap=new HashMap<>();
        courseMap.put("projectDate",projectDate);
        courseMap.put("dutyTeacherId",dutyTeacherId);
        courseMap.put("classRome",classRome);
        courseMap.put("signInTime",signInTime);
        courseMap.put("signOutTime",signOutTime);
        courseMap.put("remark",remark);

        if (updateTime(projectDate, signInTime) > 0) {//判断时间是否过期
            List<HashMap<String,Object>> dateMaps=insertCourseGchMapper.SelDateByClassRoom(classRome);//获取当期教室上课时间
            if (judgeDateTime(dateMaps,classRome, projectDate, signInTime,signOutTime) > 0) {//时间冲突判断
                if (judgeInAndOutTime(signInTime,signOutTime)>0) {//判断签退时间是否在签到时间之前
                    if (haveTeacher(trainTeachers) == 0) {//判断教师是否存在/是否停业
                        if (dutyTeacherIdInTrainTeachers(dutyTeacherId, trainTeachers) > 0) {//判断责任老师是否在实训老师之中
                            Integer courseId = SelCourseId(courseName);//获取新增的课程id
                            courseMap.put("courseId", courseId);
                            InsertCourseOn(courseMap);  //新增课程安排
                            Integer courseOnId = SelCourseOnIdBy(courseMap);//获取新增课程安排的id
                            for (Long trainTeacher : trainTeachers) {
                                InsertCourseAndTrainTeacher(courseOnId, trainTeacher);//新增课程安排与实训老师对应关系
                            }
                            return Result.ok().message("新增成功");
                        } else return Result.error().message("责任老师必须在实训老师之中");
                    } else return Result.error().message("ID为:" + haveTeacher(trainTeachers) + "的教师不存在或已被停业");
                }else return Result.error().message("签退时间不能早于签到时间");
            }
            else return Result.error().message("该教室在该时间内已被占用");
        }
        else return Result.error().message("该时间已过期");
    }

}
